﻿using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

namespace Importer
{
  public class CodeGen
  {
    public static void Do()
    {
      using (var writer = new StreamWriter(@"..\..\OldModel.cs", false, Encoding.Unicode))
      {
        Console.SetOut(writer);
        DoConsole();
      }
    }

    public static void DoConsole()
    {
      var tables = Directory.GetFiles(@"..\..\OldData", "*.txt").Select(
        s =>
          {
            var file = Path.GetFileNameWithoutExtension(s);
            Debug.Assert(file != null, "file != null");
            var arr = file.Replace(".DB", "").Split('!');
            var type = typeof (int).Assembly.GetType(arr[3]);
            var isReq = bool.Parse(arr[4]);
            var typeStr = type.IsValueType && !isReq ? type.FullName + "?" : type.FullName;
            return new
                     {
                       TableName = arr[0].Replace("-", "_").Replace(".", "_"),
                       ColumnIndex = Int32.Parse(arr[1]),
                       ColumnName = arr[2],
                       ColumnType = typeStr,
                       Dir = Path.GetDirectoryName(s),
                       File = Path.GetFileName(s)
                     };
          })
        .OrderBy(t => t.ColumnIndex)
        .GroupBy(t => t.TableName);

//      foreach (var table in tables)
//      {
//        Console.WriteLine(table.Key);
//        foreach (var column in table)
//        {
//          Console.WriteLine("\t{0} : {1}", 
//            column.ColumnName, column.ColumnType);
//        }
//      }
// ReSharper disable LocalizableElement
      Console.ReadKey();
      Console.WriteLine("#if OldModel");
      Console.WriteLine("using System.ComponentModel.Composition;");
      Console.WriteLine();
      Console.WriteLine("namespace {0}", typeof(Program).Namespace);
      Console.WriteLine("{");
      foreach (var table in tables)
      {
        Console.WriteLine("  [Export(typeof(ITable))]");
        Console.WriteLine("  [Export(typeof({0}))]", table.Key);
        Console.WriteLine("  public class {0} : Table<{0}.RowImpl>", table.Key);
        Console.WriteLine("  {");
        Console.WriteLine("     public override string Name {{ get {{ return \"{0}\"; }} }}", table.Key);
        Console.WriteLine("     protected override Column[] ColumnDefinitions()");
        Console.WriteLine("     {");
        Console.WriteLine("       return new[]");
        Console.WriteLine("         {");
        foreach (var column in table)
          Console.WriteLine("           new Column(\"{0}\", \"{1}\", typeof({2})),", column.ColumnName, column.File, column.ColumnType);
        Console.WriteLine("         };");
        Console.WriteLine("     }");
        Console.WriteLine("     protected override string Dir {{ get {{ return @\"{0}\"; }} }}", table.First().Dir);
        Console.WriteLine("     protected override RowImpl CreateRow(ITable owner)");
        Console.WriteLine("     {");
        Console.WriteLine("       return new RowImpl(owner);");
        Console.WriteLine("     }");
        Console.WriteLine("     public class RowImpl : Row");
        Console.WriteLine("     {");
        Console.WriteLine("       public RowImpl(ITable owner) : base(owner) {}");
        foreach (var column in table)
          Console.WriteLine("       public {2} {0} {{ get {{ return ({2})ConvertFromString({1}); }} }}", column.ColumnName, column.ColumnIndex, column.ColumnType);
        Console.WriteLine("     }");
        Console.WriteLine("  }");
        Console.WriteLine();
      }
      Console.WriteLine("}");
      Console.WriteLine("#endif");
// ReSharper restore LocalizableElement

    }
  }
}